<?php


namespace App\Http\Controllers;

use App\Repositories\User\CidRepo;
use App\Services\UserService;

/**
 * 用户管理-控制器
 * @author aaron
 * @since 2020/11/11
 * Class UserController
 * @package App\Http\Controllers
 */
class UserController extends Backend
{
    /**
     * 构造函数
     * @author aaron
     * @since 2020/11/11
     * UserController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->service = new UserService();
    }

    /**
     * @OA\Get(
     *     path="/user/index",
     *     tags={"User"},
     *     summary="获取用户数据",
     *     description="获取用户数据",
     *     operationId="user_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="deptID", in="query", description="部门ID", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="positionID", in="query", description="岗位ID", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="gender", in="query", description="性别", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="username", in="query", description="用户账号", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="realname", in="query", description="用户姓名", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10,type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index()
    {
        return parent::index(); // TODO: Change the autogenerated stub
    }

    /**
     * @OA\Post(
     *     path="/user/cid",
     *     operationId="user_cid",
     *     tags={"User"},
     *     summary="提交登录用户CID",
     *     description="提交登录用户CID",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"device_id", "platform"},
     *              @OA\Property(property="device_id", type="string", example="设备ID", description="设备ID"),
     *              @OA\Property(property="platform", type="string", example="平台类型", description="平台类型")
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function cid()
    {
        $deviceId = request()->get('device_id') ?? '';
        $platform = request()->get('platform') ?? '';
        CidRepo::addCid(registry('user_id'), $platform, $deviceId);
        return message('记录成功', true);
    }

    /**
     * @OA\Post(
     *     path="/user/logout",
     *     operationId="user_logout",
     *     tags={"User"},
     *     summary="退出登录",
     *     description="退出登录",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *         description="表单数据",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     *                 required={"device_id"},
     *                 @OA\Property(property="device_id", description="登录设备ID", type="string")
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function logout()
    {
        try {
            $this->service->logout();
            return message('退出成功', true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * 重置密码
     * @return mixed
     * @since 2020/11/11
     * @author aaron
     */
    public function resetPwd()
    {
        $result = $this->service->resetPwd();
        return $result;
    }

    /**
     * 根据岗位获取用户列表
     * @return mixed
     * @since 2020/11/11
     * @author aaron
     */
    public function getUserByPosition()
    {
        return $this->service->getUserByPosition();
    }
}
